var examData;
var nurseid=''
var chapterObj=(function ($) {
	var examPull;
	var start = 0,Limit = 20;
	var examPulling = false;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	function getExamData(){
		if (examPulling) {
			examPull.endPullUpToRefresh()
			return;
		}
		examPulling = true
		
		var pdict = {'userId':nurseid,'par':'0','start':start,'limit':Limit}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:FindLearnTheoryTest','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				examPull.endPullUpToRefresh();
				examPulling = false
				mui.toast(result.msg)
				return;
			}
			
			var dataArr = result['data']
			//dataArr = [{"rw":"1","TheoryDesc":"三基理论","children":[{"rw":"3","TheoryDesc":"护理程序","SingleNum":"5","MultipleNum":"0","BlankNum":"3","AllNum":"8","AnswerNum":"0","ErrorNum":"0"},{"rw":"4","TheoryDesc":"护理执业防护","SingleNum":"11","MultipleNum":"3","BlankNum":"0","AllNum":"14","AnswerNum":"0","ErrorNum":"0"},{"rw":"5","TheoryDesc":"医院和住院环境","children":[{"rw":"10","TheoryDesc":"三级节点测试","SingleNum":"1","MultipleNum":"0","BlankNum":"0","AllNum":"1","AnswerNum":"0","ErrorNum":"0"}],"SingleNum":"1","MultipleNum":"0","BlankNum":"0","AllNum":"1","AnswerNum":"0","ErrorNum":""}],"SingleNum":"17","MultipleNum":"3","BlankNum":"3","AllNum":"23","AnswerNum":"0","ErrorNum":""},{"rw":"2","TheoryDesc":"专科理论","children":[{"rw":"6","TheoryDesc":"消化系统解剖生理","SingleNum":"6","MultipleNum":"0","BlankNum":"0","AllNum":"6","AnswerNum":"0","ErrorNum":"0"}],"SingleNum":"6","MultipleNum":"0","BlankNum":"0","AllNum":"6","AnswerNum":"0","ErrorNum":""},{"rw":"7","TheoryDesc":"应急预案","SingleNum":"5","MultipleNum":"0","BlankNum":"0","AllNum":"5","AnswerNum":"0","ErrorNum":"0"},{"rw":"9","TheoryDesc":"中医","SingleNum":"0","MultipleNum":"0","BlankNum":"0","AllNum":"0","AnswerNum":"0","ErrorNum":"0"}]
			if (start == '') {
				clearBtnTapped('UL')
				mui('#UL')[0].innerHTML = ''
				setupBtnTapped('UL')
			}
			if (!dataArr) {
				examPull.endPullUpToRefresh();
				examPulling = false
				mui.toast('缺少data字段')
				return;
			}
			if (Limit > dataArr.length) {
				examPull.endPullUpToRefresh(true);
			}else{
				examPull.endPullUpToRefresh();
			}
			examData = dataArr
			updateExamUI(dataArr,'UL')
			start += Limit;
			examPulling = false
		}, function(errorStr) {
		 	mui.toast(result.msg)
		 	examPull.endPullUpToRefresh();
			examPulling = false
		});
	}

	
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var perInfo = app.getState()
		nurseid = perInfo['loginID']
		setupBtnTapped('UL')
		// mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
		// 	if (document.getElementById('search').value != "") {
		// 		document.getElementById('search').value = ""
		// 	}
		// 	resetRefresh()
		// });
		
		// var search = document.getElementById('search');
		// search.addEventListener('input',function(){
		// 	resetRefresh()
		// });
		var examScroll = document.getElementById('scroll');
		examPull = $(examScroll).pullToRefresh({
			up: {
				callback: function() {
					getExamData()
				}
			}
		});
		getExamData();
	}
	
	function resetRefresh(){
		start = 0;
		examData = []
		clearBtnTapped('UL')
		mui('#UL')[0].innerHTML = ''
		if (examPull) {
			examPull.refresh(true)
		}
		getExamData();
		$('#wrapper').scroll().scrollTo(0,0,100);
	}
	var refreshPageData = getExamData
	var exportObj = {}
	exportObj.refreshPageData = refreshPageData
	return exportObj
})(mui);

function refreshPageData(){
	chapterObj.refreshPageData()
}
function toExam(){
	var indexStr = this.getAttribute('data-index')
	var firstChapter = examData[parseInt(indexStr)] //一级数据
	var data = {rw:this.getAttribute('data-rw'),'nurseid':nurseid,'firstChapter':firstChapter}
	var webview = plus.webview.create('examChapterPaper.html', 'examChapterPaper', {
		background: '#fff',
		statusbar: {
			background: '#fff'
		}
	},{sourceData:data,fromPage:'examChapter'});
	webview.show('fade-in');
}